﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Coslen.Framework.Helpers;
using Coslen.Framework.Logging.Repository;
using Coslen.Framework.Logging.Services.Interfaces;

namespace Coslen.Framework.Logging.Services
{
    public class LoggingService : ILoggingService
    {
        private readonly ILogReportingFacade logReportingFacade;

        public LoggingService()
        {
            logReportingFacade = new LogReportingFacade();
        }

        public LoggingService(ILogReportingFacade logReportingFacade)
        {
            logReportingFacade = logReportingFacade;
        }

        public LoggingIndexModel GetLoggingIndexModel(string Period, string LoggerProviderName, string LogLevel, int? page, int? PageSize)
        {
            // Set up our default values
            string defaultPeriod = string.IsNullOrWhiteSpace(Period) ? "Today" : Period;
            string defaultLogType = string.IsNullOrWhiteSpace(LoggerProviderName) ? "All" : LoggerProviderName;
            string defaultLogLevel = string.IsNullOrWhiteSpace(LogLevel) ? "Error" : LogLevel;

            // Set up our view model
            LoggingIndexModel model = new LoggingIndexModel();

            model.Period = defaultPeriod;
            model.LoggerProviderName = defaultLogType;
            model.LogLevel = defaultLogLevel;
            model.CurrentPageIndex = page.HasValue ? page.Value -1 : 0;
            model.PageSize = PageSize.HasValue ? PageSize.Value : 20;

            TimePeriod timePeriod = TimePeriodHelper.GetUtcTimePeriod(model.Period);

            // Grab the data from the database
            model.LogEvents = logReportingFacade.GetByDateRangeAndType(model.CurrentPageIndex, model.PageSize, timePeriod.Start, timePeriod.End, model.LoggerProviderName, model.LogLevel);
            model.TotalPageCount = model.LogEvents.PageCount;
            model.TotalItemCount = model.LogEvents.TotalItemCount;

            // Fill the Select Lists

            model.LogLevelList = new List<SelectListItem>(FormsHelper.LogLevels);
            model.LoggerProviderNameList = new List<SelectListItem>(FormsHelper.LogProviderNames);
            model.PageSizeList = new List<SelectListItem>(FormsHelper.PagingPageSizes);
            model.PeriodList = new List<SelectListItem>(FormsHelper.CommonTimePeriodsHistoric);

            // Return the model
            return model;
        }

        public LogEventModel GetById(string logProviderName, string id)
        {
            LogEvent logEvent = logReportingFacade.GetById(logProviderName, id);

            LogEventModel model = new LogEventModel();
            model.LogEvent = logEvent;

            return model;
        }

        public void ClearLog(string logProviderName, DateTime start, DateTime end, string[] logLevels)
        {
            logReportingFacade.ClearLog(logProviderName, start, end, logLevels);
        }

        public LoggingManageModel GetNewLoggingManageModel()
        {
            LoggingManageModel model =  new LoggingManageModel();
            PopulateListForLoggingManageModel(model);
            return model;
        }

        public void PopulateListForLoggingManageModel(LoggingManageModel model)
        {
            model.LogLevelList = new List<SelectListItem>(FormsHelper.LogLevels);
            model.LoggerProviderNameList = new List<SelectListItem>(FormsHelper.LogProviderNames);
        }
    }
}
